let Cesium:any=undefined
/**
 * 动态墙材质
 * @private
 * @param {object} options
 * @param {string} options.image - 图片url
 * @param {number} options.duration - 持续时长
 * @param {Color} options.color - 颜色
 * @param {number} options.count - 频率
 * @param {string} options.freely - 方向，可选["vertical"|"cross"]
 * @param {string} options.direction - 方向,可选["-"|"+"]
 */
export class  WallLinkCustomMaterialProperty extends Cesium.Property{
  private  _definitionChanged:any;
  private  _color:any;
  private  _colorSubscription:any;
    private  _time:any;
  public  duration:any;
  public MaterialType:any;
  public image:any;
    constructor(options:any) {
        super();
        this._definitionChanged = new Cesium.Event();
        this._color =  options.color || Cesium.Color.BLUE;
        this._colorSubscription = undefined;
        this._time = new Date().getTime();
        this.duration = options.duration || 3000;
        this.MaterialType= options.MaterialType || "wallType2" + new Date().getTime();
        this.image=options.image;
    }
    get isvarant(){
        return false;
    }
   get definitionChanged(){
        return this._definitionChanged;
   }
   get color(){
        return this._color;
   }
   set color(value){
        this._color=value;
   }
    getType(){
        return this.MaterialType;
    }
    getValue(time:any, result:any){
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
        result.image = this.image;

        result.time = ((new Date().getTime() - this._time) % this.duration) / this.duration;
        return result;
    }
    equals(other:any){
        return (
            this === other || (other instanceof WallLinkCustomMaterialProperty && Cesium.Property.equals(this._color, other._color))
        );
    }
}

//带方向的墙体
function _getDirectionWallShader (options:any) {
    if (options && options.get) {
        var materail =
            "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
                      {\n\
                      czm_material material = czm_getDefaultMaterial(materialInput);\n\
                      vec2 st = materialInput.st;\n\
                      \n ";
        if (options.freely == "vertical") {
            //（由下到上）
            materail +=
                "vec4 colorImage = texture(image, vec2(fract(float(" +
                options.count +
                ")*st.t " +
                options.direction +
                " time), fract(st.s)));\n ";
        } else {
            //（direction顺[-]/逆[+]时针）
            materail +=
                "vec4 colorImage = texture(image, vec2(fract(float(" +
                options.count +
                ")*st.s " +
                options.direction +
                " time), fract(st.t)));\n ";
        }
        //泛光
        materail +=
            "vec4 fragColor;\n\
                      fragColor.rgb = (colorImage.rgb+color.rgb) / 1.0;\n\
                      fragColor = czm_gammaCorrect(fragColor);\n ";

        materail +=
            " material.diffuse = colorImage.rgb;\n\
                      material.alpha = colorImage.a;\n\
                      material.emission = fragColor.rgb;\n\
                      \n\
                      return material;\n\
                      }\n\
                      ";

        return materail;
    }
}

const  _initWallCustomMaterialProperty=(options:any,CesiumSources:any)=>{
    Cesium=CesiumSources
    //动态墙
    Cesium.Material._materialCache.addMaterial(options.MaterialType, {
        fabric: {
            type: options.MaterialType,
            uniforms: {
                color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
                image: options.image,
                time: 0,
            },
            source:_getDirectionWallShader({
                get: true,
                count: options.count,
                freely: options.freely,
                direction: options.direction,
            }),
        },
        translucent: function () {
            return true;
        },
    });
    return new WallLinkCustomMaterialProperty(options);
}

export  {
    _initWallCustomMaterialProperty
}
